package com.darkduck.fcap;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.os.SystemClock;
import android.util.Log;
import android.widget.SlidingDrawer;

import com.darkduck.fcap.camera.CameraManager;

public class CaptureActivityHandler extends Handler {
    
    private static final String TAG = CaptureActivityHandler.class.getSimpleName();
    
    private final CaptureActivity activity;
    private final DecodeThread decodeThread;
    
    private State state;
    private enum State {
        PREVIEW, 
        SUCCESS, 
        DONE
    }
    
    //true: auto complete, false: auto not complete
    private boolean autoState;
    
    public CaptureActivityHandler(CaptureActivity activity) {
        this.activity = activity;
        decodeThread = new DecodeThread(activity);
        decodeThread.start();
        state = State.SUCCESS;

        //Start ourselves capturing previews and decoding.
        CameraManager.get().startPreview();
        restartPreviewAndDecode();
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case R.id.auto_focus:
                
                if (state == State.PREVIEW) {
                    //decode when autofocus success
                    if (msg.obj.equals(true)) {
                        CameraManager.get().requestPreviewFrame(decodeThread.getHandler(),
                                R.id.decode);
                    }
                    //auto focus continuous when not success
                    else {
                        CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
                    }
                }             
                break;
                
            case R.id.decode_succeeded:
                Log.d(TAG, "Got decode succeeded message");
                state = State.SUCCESS;
                Bundle bundle = msg.getData();
                ArrayList<Parcelable> faceList = bundle == null ? null : bundle
                        .getParcelableArrayList(DecodeThread.BITMAP_ARRAY);
                activity.handleDecode(msg.obj, faceList);
                break;
                
            case R.id.decode_failed:
                // We're decoding as fast as possible, so when one decode fails, start another.
                state = State.PREVIEW;
                //auto focus state
                CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
                break;
                
            case R.id.restart_preview:
                Log.d(TAG, "Got restart preview message");
                restartPreviewAndDecode();
                break;
        }
    }

    private void restartPreviewAndDecode() {
        if (state == State.SUCCESS && !activity.getResultVisibleState()) {
            state = State.PREVIEW;
            autoState = false;

            // request preview frame after auto focus succeed
            CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
        }
    }

    public void quitSynchronously() {
        state = State.DONE;
        CameraManager.get().stopPreview();
        Message quit = Message.obtain(decodeThread.getHandler(), R.id.quit);
        quit.sendToTarget();
        try {
            decodeThread.join();
        } catch (InterruptedException e) {
            // continue
        }

        // Be absolutely sure we don't send any queued up messages
        removeMessages(R.id.decode_succeeded);
        removeMessages(R.id.decode_failed);
    }
}
